package com.example.mobile2021606144ywr0616;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private TextView tvMainConnState;
    private TextView tvMainReadOrWrite;
    private TextView tvMainVersion;
    private TextView tvMainPath;
    private Button btnMainOpenDB;
    private Button btnMainCloseDB;
    private MyDataAdapter dataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        initView();
        dataAdapter =new MyDataAdapter();
        btnMainOpenDB.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataAdapter.openDB( MainActivity.this );
                if (dataAdapter.isOpen()){
                    tvMainConnState.setText( "数据库已打开" );
                    tvMainPath.setText( "数据库文件路径："+dataAdapter.getPath() );
                    tvMainVersion.setText( "数据库版本："+dataAdapter.getVersion() );
                    tvMainReadOrWrite.setText( "数据库"+(dataAdapter.isReadOnly()?"只读":"可写") );
                }

            }
        } );
        btnMainCloseDB.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataAdapter.closeDB();
                tvMainReadOrWrite.setText( "数据库已关闭" );
                tvMainVersion.setText( "数据库已关闭" );
                tvMainPath.setText( "数据库已关闭" );
                tvMainConnState.setText( "数据库已关闭" );
            }
        } );
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById( R.id.tvMainTitle );
        tvMainConnState = (TextView) findViewById( R.id.tvMainConnState );
        tvMainReadOrWrite = (TextView) findViewById( R.id.tvMainReadOrWrite );
        tvMainVersion = (TextView) findViewById( R.id.tvMainVersion );
        tvMainPath = (TextView) findViewById( R.id.tvMainPath );
        btnMainOpenDB = (Button) findViewById( R.id.btnMainOpenDB );
        btnMainCloseDB = (Button) findViewById( R.id.btnMainCloseDB );
    }
}